//
//  CustomInputView.m
//  DebugDemo
//
//  Created by XFB on 2019/1/11.
//  Copyright © 2019年 ZhouCheng. All rights reserved.
//

#import "CustomInputView.h"

@interface CustomInputView ()

/**
 占位文字view: 直接让占位文字view = 当前textView，文字就会重叠
 */
@property (nonatomic, weak) UITextView *placeholderView;
/**
 文字高度
 */
@property (nonatomic, assign) CGFloat textHeight;
/**
 文字的最大高度
 */
@property (nonatomic, assign) NSInteger maxTextHeight;

@end

@implementation CustomInputView

- (UITextView *)placeholderView
{
    if (_placeholderView == nil) {
        UITextView *placeholderView = [[UITextView alloc] init];
        _placeholderView = placeholderView;
        _placeholderView.scrollEnabled = NO;
        _placeholderView.showsVerticalScrollIndicator = NO;
        _placeholderView.showsHorizontalScrollIndicator = NO;
        _placeholderView.userInteractionEnabled = NO;
        _placeholderView.font = self.font;
        _placeholderView.textColor = UIColorFromRGB(0xCCCCCC);
        _placeholderView.backgroundColor = [UIColor clearColor];
        [self addSubview:placeholderView];
    }
    return _placeholderView;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup
{
    self.scrollEnabled = NO;
    self.scrollsToTop = NO;
    
    self.showsHorizontalScrollIndicator = NO;
    self.enablesReturnKeyAutomatically = YES;
    
    self.layer.cornerRadius = 5;
    self.layer.masksToBounds = YES;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChange) name:UITextViewTextDidChangeNotification object:self];
}

- (void)setCornerRadius:(NSUInteger)cornerRadius
{
    _cornerRadius = cornerRadius;
    self.layer.cornerRadius = cornerRadius;
}

- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    self.placeholderView.text = placeholder;
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
    _placeholderColor = placeholderColor;
    self.placeholderView.textColor = placeholderColor;
}

- (void)setTextHeightChangeBlock:(void (^)(NSString * _Nonnull, CGFloat))TextHeightChangeBlock
{
    _TextHeightChangeBlock = TextHeightChangeBlock;
    [self textViewDidChange];
}

- (void)setMaxNumberOfLines:(NSInteger)maxNumberOfLines
{
    _maxNumberOfLines = maxNumberOfLines;
    /// 计算最大高度 = （每行高度 * 总行数 + 文字上下间距）
    _maxTextHeight = ceilf(self.font.lineHeight * maxNumberOfLines + self.textContainerInset.top + self.textContainerInset.bottom);
}

- (void)textViewDidChange
{
    /// 占位文字是否显示
    self.placeholderView.hidden = self.text.length > 0;
    
    CGFloat height = ceilf([self sizeThatFits:CGSizeMake(self.bounds.size.width, MAXFLOAT)].height);

    if (_textHeight != height) { /// 高度不一样，就改变了高度
        /// 达到最大高度，可以滚动
        self.scrollEnabled = height>_maxTextHeight && _maxTextHeight > 0;
        
        _textHeight = height;
        
        if (_TextHeightChangeBlock && self.scrollEnabled == NO) {
            _TextHeightChangeBlock(self.text, height);
            [self.superview layoutIfNeeded];
            self.placeholderView.frame = self.bounds;
        }
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
